web/src/pages/[category].astro

103 lines · 2433 bytes

  1---
  2
  3import Layout from '@layouts/Layout.astro';
  4import SectionList from '@components/things/SectionList.astro';
  5import Main from '@components/scafold/MainCard.astro';
  6
  7import { fetchData, slugify } from '@utils/fetch-data';
  8
  9import type { Section } from '../types/Service';
 10import Button from '@components/form/Button.astro';
 11
 12interface Props {
 13	title: string;
 14	sections: Section[];
 15}
 16
 17const { title, sections } = Astro.props;
 18
 19const categories = (await fetchData()).categories;
 20
 21export async function getStaticPaths() {
 22	const pages = await fetchData().then((data) => {
 23		return data.categories.map((category) => {
 24			return {
 25				category: slugify(category.name),
 26				title: category.name,
 27				sections: category.sections,
 28			};
 29		});
 30	});
 31
 32  return pages.map(({ category, title, sections }) => {
 33    return {
 34      params: { category },
 35      props: { title, sections },
 36    };
 37  });
 38}
 39
 40const makePaginationLinks = () => {
 41  const index = categories.findIndex(category => category.name === title);
 42  const previousCategory = index >  0 ? categories[index -  1].name : null;
 43  const nextCategory = index < categories.length -  1 ? categories[index +  1].name : null;
 44  return { previous: previousCategory, next: nextCategory };
 45};
 46
 47const { previous, next } = makePaginationLinks();
 48
 49---
 50
 51<Layout title={`${title} | Awesome Privacy`}>
 52	<Main>
 53		<SectionList title={title} sections={sections} bigTitle={true} />
 54    <a href={`/all#${slugify(title)}`}>Browse All</a>
 55		<div class="pagination-navigation">
 56      { previous ? (
 57        <Button url={`/${slugify(previous)}`}>
 58          <span>← Previous</span>
 59          <p>{previous}</p>
 60        </Button>
 61      ) : <p class="nothing"></p>}
 62      { next && (
 63        <Button url={`/${slugify(next)}`}>
 64          <span>Next →</span>
 65          <p>{next}</p>
 66        </Button>
 67      )}
 68    </div>
 69	</Main>
 70</Layout>
 71
 72<style lang="scss">
 73.pagination-navigation {
 74  display: flex;
 75  justify-content: space-between;
 76  align-items: center;
 77  padding: 1rem 0;
 78  :global(.button) {
 79    min-width: 120px;
 80    width: fit-content;
 81    padding: 0.25rem 1rem;
 82    text-align: right;
 83    &:first-child { text-align: left; }
 84    p {
 85      margin: 0;
 86      font-weight: normal;
 87      font-size: 1rem;
 88    }
 89    span {
 90      font-size: 0.8rem;
 91    }
 92    
 93  }
 94  .nothing {
 95    width: 120px;
 96  }
 97  .go-to-category {
 98    color: var(--foreground);
 99    font-size: 0.8rem;
100    opacity: 0.5;
101  }
102}
103</style>